<?php

class SlideForm extends BaseForm {
  const FORM_NAME           = 'frm';

  const FIELD_HEADING_MAIN  = 'heading_main';
  const FIELD_HEADING_SUB   = 'heading_sub';
  const FIELD_IMAGE         = 'image';
  const FIELD_TEMPLATE      = 'template';
  const FIELD_DESCRIPTION   = 'description';
  const FIELD_COURSE        = 'course';
  const FIELD_STATUS        = 'status';

  public function configure() {
    $arrTemplatesList = StringUtil::getTemplatesArray();
    $arrStatusList    = StringUtil::getStatusArray();
    $arrCourseList    = COURSETable::getInstance()->getCoursesArray();

    $widget = array(
            self::FIELD_HEADING_MAIN   => new sfWidgetFormInputText(array(), array('maxlength' => 255)),
            self::FIELD_HEADING_SUB    => new sfWidgetFormInputText(array(), array('maxlength' => 255)),
            self::FIELD_IMAGE          => new sfWidgetFormInputFile(array(), array()),
            self::FIELD_TEMPLATE       => new sfWidgetFormSelect(array('choices' => $arrTemplatesList), array('multiple' => false, 'expand' => true)),
            self::FIELD_DESCRIPTION    => new sfWidgetFormTextareaTinyMCE(array(), array()),
            self::FIELD_STATUS         => new sfWidgetFormSelect(array('choices' => $arrStatusList), array('multiple' => false, 'expand' => true)),
            self::FIELD_COURSE         => new sfWidgetFormSelect(array('choices' => $arrCourseList), array('multiple' => false, 'expand' => true))

    );

    $labels = array(
            self::FIELD_HEADING_MAIN   => 'Main Heading<req />',
            self::FIELD_HEADING_SUB    => 'Sub Heading<req />',
            self::FIELD_IMAGE          => 'Image<req />',
            self::FIELD_TEMPLATE       => 'Template<req />',
            self::FIELD_DESCRIPTION    => 'Description<req />',
            self::FIELD_STATUS         => 'Status<req />'
    );

    $this->setWidgets($widget);
    $this->widgetSchema->setLabels($labels);

    $validators = array(
            self::FIELD_HEADING_MAIN   => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_HEADING_MAIN])))),
            self::FIELD_TEMPLATE   => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_TEMPLATE])))),
            self::FIELD_DESCRIPTION => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_DESCRIPTION])))),
            self::FIELD_STATUS      => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_STATUS]))))
    );

    $this->setValidators($validators);
    //$this->validatorSchema->setPostValidator(new sfValidatorCallback(array('callback' => array($this, 'postValidator'))));

    $this->widgetSchema->setNameFormat(self::FORM_NAME . '[%s]');

    $this->validatorSchema->setOption('allow_extra_fields', true);
    $this->validatorSchema->setOption('filter_extra_fields', false);
  }

  public function postValidator($validator, $values) {
    return $values;
  }

  public function populateData(SLIDE $slide) {
    $this->setDefault(self::FIELD_COURSE, $slide->getCourseId());
    $this->setDefault(self::FIELD_HEADING_MAIN, $slide->getHeadingMain());
    $this->setDefault(self::FIELD_HEADING_SUB, $slide->getHeadingSub());
    $this->setDefault(self::FIELD_DESCRIPTION, $slide->getDescription());
    $this->setDefault(self::FIELD_TEMPLATE, $slide->getTemplate());
    $this->setDefault(self::FIELD_STATUS, $slide->getIsActive());
  }
}
?>